﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyHocPhi.BLL;
using QuanLyHocPhi.Model;
using QuanLyHocPhi.GUI.DuLieu;
namespace QuanLyHocPhi.GUI.TacNghiep
{
    public partial class FormQuanLyPhieuThu : FormBase
    {
        bool lockUpdate = false;
        List<ThongKePhieuThu> danhSach;
        public FormQuanLyPhieuThu()
        {
            InitializeComponent();
            if (Program.NguoiSuDung.VaiTro != (int)VaiTroNguoiSuDung.TacNghiep)
            {
                colXoaPhieu.Visible = bntChinhSuaLop.Visible =
                colChinhSua.Visible = true;
            }
            else
            {
                colXoaPhieu.Visible = bntChinhSuaLop.Visible =
                colChinhSua.Visible = false;
            }

            #region Cấu hình lưới
            colMaPhieuThu.DataPropertyName = "MaPhieuThu";
            colMaSV.DataPropertyName = "MaSinhVien";
            colHo.DataPropertyName = "HoDem";
            colTen.DataPropertyName = "Ten";
            colQuyen.DataPropertyName = "SoQuyen";
            colSoPhieu.DataPropertyName = "SoPhieuNew";
            colSoTien.DataPropertyName = "SoTienStr";
            colNgayThu.DataPropertyName = "NgayNopStr";
            colXoaPhieu.DataPropertyName = "ThaoTacXoa";
            colChinhSua.DataPropertyName = "ThaoTacChinhSua";
            colNoiDungThu.DataPropertyName = "NoiDungNop";
            gridPhieuThu.AllowUserToAddRows = false;
            gridPhieuThu.AllowUserToDeleteRows = false;
            gridPhieuThu.AutoGenerateColumns = false;
            #endregion

            LoadNamHoc();
            LoadLopGoc();
        }

        private void QuanLyPhieuThu_Load(object sender, EventArgs e)
        {
        }

        private void gridQuaTrinhNopHocPhi_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex == colXoaPhieu.Index)
            {
                if (DialogUtils.ShowYesNo("Bạn có chắc muốn xóa") == System.Windows.Forms.DialogResult.Yes)
                {
                    var pt = bdsPhieuThu.Current as ThongKePhieuThu;
                    if (BizPhieuThu.Delete(pt.MaPhieuThu))
                    {
                        bdsPhieuThu.RemoveCurrent();
                        DialogUtils.ShowMessage("Đã xóa");
                    }
                    else
                        DialogUtils.ShowError("Thao tác xóa thất bại");
                }
                else
                    DialogUtils.ShowMessage("Bỏ qua");
            }
            else if (e.RowIndex != -1 && e.ColumnIndex == colChinhSua.Index)
            {
                var thongKePhieuThu = bdsPhieuThu.Current as ThongKePhieuThu;
                var pt = BizPhieuThu.GetPhieuThuFull(thongKePhieuThu.MaPhieuThu);
                FormChiTietPhieuThu frm = new FormChiTietPhieuThu(pt);
                frm.ShowDialog();
                //bntOKQuyen.PerformClick();
            }
        }

        private void bntOKQuyen_Click(object sender, EventArgs e)
        {
            FormProgress frm = new FormProgress();
            frm.DoWork += delegate(object oSender, DoWorkEventArgs oE)
            {
                danhSach = BizPhieuThu.GetListPhieuThu(txtQuyenSo.Text, txtSoPhieu.Text);
            };
            frm.RunWorkerCompleted += delegate(object oSender, RunWorkerCompletedEventArgs oE)
            {
                if (oE.Error != null)
                {
                    string msg = oE.Error.Message;
                    if (oE.Error.InnerException != null)
                        msg += oE.Error.InnerException.Message;
                    DialogUtils.ShowError(msg);
                }
            };
            frm.StartWorking(this);
            bdsPhieuThu.DataSource = danhSach;
            gridPhieuThu.ReadOnly = true;
            gridPhieuThu.DataSource = bdsPhieuThu;

        }

        private void txtSoPhieu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
                bntOKQuyen.PerformClick();
        }

        private void txtQuyenSo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                bntOKQuyen.PerformClick();
                return;
            }
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void bntChinhSuaLop_Click(object sender, EventArgs e)
        {
            FormChinhSuaNgayThu frm = new FormChinhSuaNgayThu(bdsPhieuThu.DataSource as List<ThongKePhieuThu>);
            if (frm.ShowDialog() != DialogResult.Cancel)
            {
                if (tabControl1.SelectedIndex == 1)
                    bntQLTheoLop.PerformClick();
                else if (tabControl1.SelectedIndex == 0)
                    bntOKQuyen.PerformClick();
            }

        }

        private void InitComboSuggestion(ComboBox cmb, object datasource, string displayMember, string valueMember, string[] rangeDisplay)
        {
            cmb.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cmb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cmb.DataSource = datasource;
            if (displayMember != null)
                cmb.DisplayMember = displayMember;
            if (valueMember != null)
                cmb.ValueMember = valueMember;
            // Xác định AutoCompleteCustomeSource
            var autoCompletSet = new AutoCompleteStringCollection();
            autoCompletSet.AddRange(rangeDisplay);
            cmb.AutoCompleteCustomSource = autoCompletSet;

            // Các tinh chỉnh nâng cao
            cmb.Leave += (oSender, oE) =>
            {
                var cbx = oSender as ComboBox;
                if (cbx == null)
                    return;
                cbx.BackColor = cbx.SelectedItem == null ? Color.LightPink : SystemColors.Window;
            };

            cmb.KeyDown += (oSender, oE) =>
            {
                if (oE.KeyCode != Keys.Enter)
                    return;
                var cbx = oSender as ComboBox;
                if (cbx == null)
                    return;
                cbx.BackColor = cbx.SelectedItem == null ? Color.LightPink : SystemColors.Window;
            };
        }

        private void LoadNamHoc()
        {
            lockUpdate = true;
            var ds = BizNamHoc.GetListNamHoc();
            InitComboSuggestion(cmbNamHoc, ds, "TenNamHoc", "TenNamHoc", ds.Select(e => e.TenNamHoc).ToArray());
            lockUpdate = false;
            LoadHocKy();
        }

        private void LoadHocKy()
        {
            lockUpdate = true;
            cmbHocKy.DataSource = null;
            if ((cmbNamHoc.SelectedItem as NamHoc) != null)
            {
                var ds = BizHocKy.GetList_HocKy((cmbNamHoc.SelectedItem as NamHoc).TenNamHoc);
                cmbHocKy.DataSource = ds;
                cmbHocKy.DisplayMember = "TenHocKy";
                cmbHocKy.ValueMember = "MaHocKy";
            }
            lockUpdate = false;
        }

        private void LoadLopGoc()
        {
            lockUpdate = true;
            cmbLop.DataSource = null;
            var ds = new List<LopGoc>();
            FormProgress frm = new FormProgress();
            frm.DoWork += delegate(object oSender, DoWorkEventArgs oE)
            {
                ds = BizLopGoc.GetListLopGoc();
            };
            #region On complete
            frm.RunWorkerCompleted += delegate(object oSender, RunWorkerCompletedEventArgs oE)
            {
                if (oE.Error != null)
                {
                    string msg = oE.Error.Message;
                    if (oE.Error.InnerException != null)
                        msg += oE.Error.InnerException.Message;
                    DialogUtils.ShowError(msg);
                }

            };
            #endregion

            frm.StartWorking(this);
            InitComboSuggestion(cmbLop, ds, "TenLopGoc", "MaLopGoc", ds.Select(e => e.TenLopGoc).ToArray());
            ds.Insert(0, new LopGoc() { MaLopGoc = "-1000", TenLopGoc = "Tất cả" });
            InitComboSuggestion(cmbLopGocTheoCaNhan, ds, "TenLopGoc", "MaLopGoc", ds.Select(e => e.TenLopGoc).ToArray());
            lockUpdate = false;
            cmbLop_SelectedIndexChanged(this, new EventArgs());
        }

        private void cmbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!lockUpdate)
            {
                LopGoc lop = cmbLop.SelectedItem as LopGoc;
                if (lop != null)
                {
                    txtLoaiHinh.Text = lop.LoaiHinhDaoTao.TenLoaiHinh;
                    txtNTS.Text = lop.NamTuyenSinh;
                }
            }
        }

        private void bntQLTheoLop_Click(object sender, EventArgs e)
        {
            if (cmbHocKy.SelectedIndex == -1 || cmbLop.SelectedIndex == -1)
            {
                DialogUtils.ShowError("Bạn chưa chọn lớp hoặc học kỳ!");
                return;
            }
            string maHocky = (cmbHocKy.SelectedItem as HocKy).MaHocKy;
            string maLop = (cmbLop.SelectedItem as LopGoc).MaLopGoc;
            FormProgress frm = new FormProgress();
            frm.DoWork += delegate(object oSender, DoWorkEventArgs oE)
            {
                danhSach = BizPhieuThu.GetListPhieuThuTheoLop(maHocky, maLop);
            };
            frm.RunWorkerCompleted += delegate(object oSender, RunWorkerCompletedEventArgs oE)
            {
                if (oE.Error != null)
                {
                    string msg = oE.Error.Message;
                    if (oE.Error.InnerException != null)
                        msg += oE.Error.InnerException.Message;
                    DialogUtils.ShowError(msg);
                }
            };
            frm.StartWorking(this);
            bdsPhieuThu.DataSource = danhSach;
            gridPhieuThu.ReadOnly = true;
            gridPhieuThu.DataSource = bdsPhieuThu;
            //}
        }

        private void cmbNamHoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!lockUpdate)
            {
                LoadHocKy();
            }
        }

        private void bntSoTien_Click(object sender, EventArgs e)
        {
            int temp = 0;
            int soTien = 0;
            string strSoTien = txtSoTien.Text.Replace(",", "").Replace(".", "");
            if (int.TryParse(strSoTien, out temp))
                soTien = int.Parse(strSoTien);
            FormProgress frm = new FormProgress();
            frm.DoWork += delegate(object oSender, DoWorkEventArgs oE)
            {
                danhSach = BizPhieuThu.GetListPhieuThu(soTien);
            };
            frm.RunWorkerCompleted += delegate(object oSender, RunWorkerCompletedEventArgs oE)
            {
                if (oE.Error != null)
                {
                    string msg = oE.Error.Message;
                    if (oE.Error.InnerException != null)
                        msg += oE.Error.InnerException.Message;
                    DialogUtils.ShowError(msg);
                }
            };
            frm.StartWorking(this);
            bdsPhieuThu.DataSource = danhSach;
            gridPhieuThu.ReadOnly = true;
            gridPhieuThu.DataSource = bdsPhieuThu;
        }

        private void txtSoTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                bntSoTien.PerformClick();
                return;
            }
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtHoTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                bntHoTen.PerformClick();
                return;
            }
        }

        private void bntHoTen_Click(object sender, EventArgs e)
        {
            string hoTen = txtHoTen.Text;
            if (cmbLopGocTheoCaNhan.SelectedIndex == -1)
                return;
            LopGoc lop = (LopGoc)cmbLopGocTheoCaNhan.SelectedItem;
            FormProgress frm = new FormProgress();
            frm.DoWork += delegate(object oSender, DoWorkEventArgs oE)
            {
                danhSach = BizPhieuThu.GetListPhieuThuTheoHoTen(hoTen, lop.MaLopGoc);
            };
            frm.RunWorkerCompleted += delegate(object oSender, RunWorkerCompletedEventArgs oE)
            {
                if (oE.Error != null)
                {
                    string msg = oE.Error.Message;
                    if (oE.Error.InnerException != null)
                        msg += oE.Error.InnerException.Message;
                    DialogUtils.ShowError(msg);
                }
            };
            frm.StartWorking(this);
            bdsPhieuThu.DataSource = danhSach;
            gridPhieuThu.ReadOnly = true;
            gridPhieuThu.DataSource = bdsPhieuThu;
        }

        private void txtSoTien_TextChanged(object sender, EventArgs e)
        {
            int temp = 0;
            var tien = txtSoTien.Text.Replace(".", "").Replace(",", "");
            if (int.TryParse(tien, out temp))
            {
                txtSoTien.Text = tien.FormatCurrency();
                txtSoTien.Select(txtSoTien.Text.Length, 0);
            }
        }
    }
}
